<?php
/**
 * Date: 2017/11/10
 * Time: 10:08
 * Author: zeronlee
 */

namespace App\Http\Controllers\Backend\Announce;


use App\Http\Controllers\Controller;
use App\Models\Access\User;
use App\Models\Announce\Announce;
use App\Models\Ogz;
use App\Models\App;
use Illuminate\Http\Request;
use Carbon\Carbon;
use App\Models\Log;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use  Illuminate\Database\Query\Builder;


class AnnounceController extends Controller
{

    public function index(Request $request)
    {
        $fields = "";
        $where = array();
        $add = array();
        $startTime = $request->startTime;
        $endTime = $request->endTime;
        $searchKeywords = htmlspecialchars($request->searchKeywords);
        if ($searchKeywords) {
            $add['searchKeywords'] = $searchKeywords;
        }
//            DB::connection()->enableQueryLog();
        $fields = Announce::join('users', 'users.id', '=', 'announce.user_id')
            ->where(function ($query) use ($where) {
                if (!empty($where)) {
                    $query->where($where);
                }
            })
            ->Where(function ($query) use ($searchKeywords) {
                if ($searchKeywords) {
                    $key = '%' . $searchKeywords . '%';
                    $query->where('announce.title', 'like', $key);
                }
            })
            ->select('announce.*', 'users.name')
            ->orderBy('announce.id', 'desc')
            ->paginate(10)
        ;

        if (!empty($add)) {
            $page = isset($page) ? $request['page'] : 1;
            $add['page'] = $page;
            $fields = $fields->appends($add);
        }

        //add more element if you have more search terms


        return view('backend.announce.index')->with('lists', $fields);
    }


    /**新增
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function newAnnounce(Request $request)
    {
        if ($request->method() == "POST") {
            $this->validate($request, [
                'description' => 'required|max:255',
                'contents' => 'required',
                'is_publish' => 'required|integer',
                'title' => 'required|max:45',
            ], [
                    "title.required" => "公告标题为必填项",
                    "title.max" => "公告标题长度不可超过45个字符",
                    "description.max" => "公告描述长度不可以超过225个字符",
                    "description.required" => "公告描述为必填项",
                    "contents.required" => "公告内容为必填项",
                    "is_publish.required" => "是否发布是必填项",
                    "is_publish.integer" => "是否发布有误",

                ]
            );
            $newdata = [
                "cate_id" => 1,
                'sn' => $this->creatAnnounceCode(),
                'title' => $request->title,
                'description' => $request->description,
                'content' => clean($request->contents, 'user_topic_body'),
                'user_id' => Auth::id(),
                'is_publish' => $request->is_publish,
            ];
            $insert = DB::table('announce')->insert(
                $newdata
            );
            return redirect('/admin/announcelist');
            exit();
        }

        return view('backend.announce.newannounce');

    }

    /**公告编号
     * @return string
     */
    private function creatAnnounceCode()
    {
        $announce = DB::table('announce')
            ->orderBy('id', 'desc')
            ->first();
        $codeY = date('Y');
        if ($announce) {
            $sn = $announce->sn;
            if ($sn) {
                $sn = explode("-", $sn);
                if ($sn[1] == $codeY) {
                    $old = $sn[2];
                    $codeSn = $old + 1;
                } else {
                    $codeSn = 1;
                }
            } else {
                $codeSn = 1;
            }
        } else {
            $codeSn = 1;
        }
        $code = "HSRC-" . $codeY . '-' . $codeSn;
        return $code;

    }


    /**发布公告
     * @param Request $request
     */
    public function publish(Request $request)
    {
        $id = intval($request->id);
        if ($id) {
            $update['is_publish'] = 1;
            $insert = DB::table('announce')
                ->where('id', $id)
                ->update($update);
        }
        return redirect('/admin/announcelist');
    }

    /**修改公告
     * @param Request $request
     */
    public function modify(Request $request)
    {
        $id = intval($request->id);
        if ($request->method() == "POST") {
            $validator = $this->validate($request, [
                'description' => 'required|max:255',
                'contents' => 'required',
                'is_publish' => 'required|integer',
                'title' => 'required|max:45',
            ], [
                    "title.required" => "公告标题为必填项",
                    "title.max" => "公告标题长度不可超过45个字符",
                    "description.max" => "公告描述长度不可以超过225个字符",
                    "description.required" => "公告描述为必填项",
                    "contents.required" => "公告内容为必填项",
                    "is_publish.required" => "是否发布是必填项",
                    "is_publish.required" => "是否发布是必填项",
                    "is_publish.integer" => "是否发布有误",

                ]
            );
            $newdata = [
                'title' => $request->title,
                'description' => $request->description,
                'content' => clean($request->contents, 'user_topic_body'),
                'updated_at' => date('Y-m-d H:i:s'),
                'is_publish' => $request->is_publish,
            ];
            $insert = DB::table('announce')
                ->where('id', $id)
                ->update($newdata);
            return redirect('/admin/announcelist');
        } else {

            if ($id) {
                $fields = DB::table('announce')
                    ->leftJoin('users', 'users.id', '=', 'announce.user_id')
                    ->select('announce.*', 'users.name')
                    ->where('announce.id', $id)
                    ->first();
            }
            return view('backend.announce.modify')->with('lists', $fields);
        }

    }

    /**删除公告
     * @param Request $request
     */
    public function del(Request $request)
    {
        $id = intval($request->id);
        if ($id) {
            $announce = Announce::find($id);
            $announce->delete();
            if ($announce->trashed()) {
                return redirect('/admin/announcelist');
            } else {
                return redirect('/admin/announcelist');
            }
        }

    }

    /**公告详情
     * @param Request $request
     */
    public function det(Request $request)
    {

        $id = intval($request->id);
        if ($id) {
            $fields = DB::table('announce')
                ->leftJoin('users', 'users.id', '=', 'announce.user_id')
                ->select('announce.*', 'users.name')
                ->where('announce.id', $id)
                ->first();
        }

        return view('backend.announce.det')->with('lists', $fields);
    }


}